using UnityEngine;

namespace Crosstales.Common.Util
{
    // [Assembly-CSharp-firstpass.dll].Crosstales.Common.Util.MemoryCacheStream
    public class MemoryCacheStream : Stream
    {
        // Fields
        private byte[] cache;
        private long writePosition;
        private long readPosition;
        private long length;
        private int size;
        private readonly int maxSize;
        
        // Properties
        public override bool CanRead { get; }
        public override bool CanSeek { get; }
        public override bool CanWrite { get; }
        public override long Position { get; set; }
        public override long Length { get; }
        
        // Methods
        public MemoryCacheStream(int cacheSize = 65536, int maxCacheSize = 67108864)
        {
            this.writePosition = 0;
            this.readPosition = 0;
            this.length = 0;
            this.size = cacheSize;
            this.maxSize = maxCacheSize;
            this.createCache();
        }
        public override bool get_CanRead()
        {
            return true;
        }
        public override bool get_CanSeek()
        {
            return true;
        }
        public override bool get_CanWrite()
        {
            return true;
        }
        public override long get_Position()
        {
            return (long)this.readPosition;
        }
        public override void set_Position(long value)
        {
            if((value & 9223372036854775808) == 0)
            {
                    this.readPosition = value;
                return;
            }
            
            27342280 = new System.ArgumentOutOfRangeException(paramName:  27548200, message:  27508992);
            throw 27342280;
        }
        public override long get_Length()
        {
            return (long)this.length;
        }
        public override void Flush()
        {
        
        }
        public override long Seek(long offset, System.IO.SeekOrigin origin)
        {
            var val_5;
            var val_6;
            var val_8;
            System.IO.IOException val_9;
            val_5 = offset;
            val_6 = this;
            if(origin == 2)
            {
                goto label_0;
            }
            
            if(origin == 1)
            {
                goto label_1;
            }
            
            if(origin != 0)
            {
                goto label_2;
            }
            
            val_8 = (long)val_5;
            goto label_3;
            label_1:
            val_8 = val_6 + val_5;
            if(origin < 0)
            {
                goto label_4;
            }
            
            label_5:
            label_3:
            val_6 = ???;
            val_5 = ???;
            goto typeof(Crosstales.Common.Util.MemoryCacheStream).__il2cppRuntimeField_1F8;
            label_0:
            var val_1 = (val_6 + 64) + val_5;
            if(origin >= 0)
            {
                goto label_5;
            }
            
            label_4:
            val_9 = 27348600;
            val_9 = new System.IO.IOException(message:  27489856);
            throw val_9 = 27342264;
            label_2:
            val_9 = new System.ArgumentException(message:  27504272);
            throw val_9;
        }
        public override void SetLength(long value)
        {
            if(this.size == value)
            {
                    return;
            }
            
            this.size = value;
            this.length = 0;
            this.createCache();
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            System.ArgumentOutOfRangeException val_3;
            string val_4;
            if((offset & 2147483648) != 0)
            {
                goto label_0;
            }
            
            if((count & 2147483648) != 0)
            {
                goto label_1;
            }
            
            Unknown val_3 = buffer.Length;
            val_3 = val_3 - offset;
            if(val_3 < count)
            {
                goto label_3;
            }
            
            if(this.readPosition >= this.length)
            {
                    return 0;
            }
            
            return this.read(buff:  buffer, offset:  offset, count:  count);
            label_0:
            val_3 = 27342280;
            val_4 = 27541688;
            goto label_5;
            label_1:
            val_3 = 27342280;
            val_4 = 27530816;
            label_5:
            val_3 = new System.ArgumentOutOfRangeException(paramName:  val_4, message:  27508992);
            throw val_3 = 27342264;
            label_3:
            val_3 = new System.ArgumentException(message:  27509992);
            throw val_3;
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            System.ArgumentOutOfRangeException val_3;
            var val_4;
            string val_5;
            string val_6;
            if((offset & 2147483648) != 0)
            {
                goto label_0;
            }
            
            if((count & 2147483648) != 0)
            {
                goto label_1;
            }
            
            if(this.size < count)
            {
                goto label_2;
            }
            
            Unknown val_3 = buffer.Length;
            val_3 = val_3 - offset;
            if(val_3 < count)
            {
                goto label_4;
            }
            
            if(count == 0)
            {
                    return;
            }
            
            this.write(buff:  buffer, offset:  offset, count:  count);
            return;
            label_0:
            val_3 = 27342280;
            val_4 = 27541688;
            goto label_6;
            label_1:
            val_3 = 27342280;
            val_4 = 27530816;
            label_6:
            val_5 = val_4;
            val_6 = 27508992;
            goto label_7;
            label_2:
            val_3 = 27342280;
            val_5 = 27530816;
            val_6 = 27523032;
            label_7:
            val_3 = new System.ArgumentOutOfRangeException(paramName:  val_5, message:  val_6);
            throw val_3 = 27342264;
            label_4:
            val_3 = new System.ArgumentException(message:  27509992);
            throw val_3;
        }
        private int read(byte[] buff, int offset, int count)
        {
            Unknown val_5;
            System.Array val_6;
            long val_5 = this.readPosition;
            long val_1 = val_5 / this.size;
            val_5 = val_5 - (val_1 * this.size);
            val_1 = val_5 + count;
            if(val_1 > this.size)
            {
                    int val_2 = this.size - val_5;
                int val_3 = count - val_2;
                System.Array.Copy(sourceArray:  this.cache, sourceIndex:  val_5, destinationArray:  buff, destinationIndex:  offset, length:  val_2);
                val_5 = this.cache;
                int val_4 = val_2 + offset;
                val_6 = buff;
            }
            else
            {
                    val_5 = this.cache;
                val_6 = buff;
            }
            
            System.Array.Copy(sourceArray:  val_5, sourceIndex:  val_5, destinationArray:  val_6, destinationIndex:  offset, length:  count);
            long val_6 = this.readPosition;
            val_6 = val_6 + (count << );
            this.readPosition = val_6;
            return (int)count;
        }
        private void write(byte[] buff, int offset, int count)
        {
            Unknown val_6;
            System.Array val_7;
            long val_6 = this.writePosition;
            long val_2 = val_6 - ((val_6 / this.size) * this.size);
            val_6 = val_2 + count;
            if(val_6 > this.size)
            {
                    int val_3 = this.size - val_2;
                int val_4 = count - val_3;
                System.Array.Copy(sourceArray:  buff, sourceIndex:  offset, destinationArray:  this.cache, destinationIndex:  val_2, length:  val_3);
                val_6 = this.cache;
                int val_5 = val_3 + offset;
                val_7 = buff;
            }
            else
            {
                    val_6 = this.cache;
                val_7 = buff;
            }
            
            System.Array.Copy(sourceArray:  val_7, sourceIndex:  offset, destinationArray:  val_6, destinationIndex:  val_2, length:  count);
            long val_7 = this.writePosition;
            val_7 = val_7 + (count << );
            this.writePosition = val_7;
            this.length = val_7;
        }
        private void createCache()
        {
            if(this.size > this.maxSize)
            {
                    this.cache = 536880965;
                UnityEngine.Debug.LogWarning(message:  -1610611951);
                return;
            }
            
            this.cache = 536880965;
        }
    
    }

}
